﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_roleMenuAdd : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        if(!IsPostBack)
        {
            BindDDL();
        }
    }

    //绑定数据源
    private void BindDDL()
    {
        UtilityDataBind.DropDownListBindData<Role>(ref rolelist,
            RoleBLL.GetInstance().GetList(), "RoleName", "RoleId", "角色");
        UtilityDataBind.DropDownListBindData<OneShoppingModel.Menu>(ref menulist,
            MenuBLL.GetInstance().GetList(r=>r.ParentMenu==0), "MenuName","MenuId","一级菜单");
    }

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("roleMenuManager.aspx");
    }
    //二级联动
    protected void menulist_SelectedIndexChanged(object sender, EventArgs e)
    {
        int parentid=Convert.ToInt32(menulist.SelectedValue);
        UtilityDataBind.DropDownListBindData<OneShoppingModel.Menu>(ref twomenulist,
            MenuBLL.GetInstance().GetList(r => r.ParentMenu == parentid), "MenuName", "MenuId", "二级菜单");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if(rolelist.SelectedValue=="-1")
        {
            msg_role.InnerText = "请选择角色";
            msg_role.Visible = true;
            return;
        }
        if(menulist.SelectedValue=="-1")
        {
            msg_menu.InnerText = "请选择菜单";
            msg_menu.Visible = true;
            return;
        }
        if (twomenulist.SelectedValue == "-1")
        {
            msg_twomenu.InnerText = "请选择菜单";
            msg_twomenu.Visible = true;
            return;
        }
        int menuid=Convert.ToInt32(menulist.SelectedValue);
        int roleid=Convert.ToInt32(rolelist.SelectedValue);
        int twomenuid = Convert.ToInt32(twomenulist.SelectedValue);
        RoleMenu t=RoleMenuBLL.GetInstance().GetEntity(r=>r.MenuId==menuid&& r.RoleId==roleid);
        if(t==null)
        {
            RoleMenu onerm = new RoleMenu
            {
                MenuId = menuid,
                RoleId = roleid
            };
            RoleMenuBLL.GetInstance().Insert(onerm);
        }
        RoleMenu q = RoleMenuBLL.GetInstance().GetEntity(r => r.MenuId == twomenuid && r.RoleId == roleid);
        if(q!=null)
        {
            msg_twomenu.InnerText = "角色权限已存在";
            msg_twomenu.Visible = true;
            return;
        }
        RoleMenu rm = new RoleMenu
        {
            MenuId = twomenuid,
            RoleId = roleid
        };
        bool rrm = RoleMenuBLL.GetInstance().Insert(rm);
        if(rrm)
        {
            UtilityMessageBox.ShowAndRedirect(this, "添加角色权限成功", "roleMenuManager.aspx");
        }
        else
        {
            UtilityMessageBox.ShowAndRedirect(this, "添加角色权限失败", "roleMenuManager.aspx");
        }
    }

    
}